package com.contexticon.Transcriber2;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.filechooser.FileFilter;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.JCheckBox;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JSeparator;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;

import uk.co.caprica.vlcj.filter.swing.SwingFileFilterFactory;

import javax.swing.AbstractAction;
import javax.swing.Action;

import com.contexticon.model.Transcript;

import uk.co.caprica.vlcj.filter.swing.SwingFileFilterFactory;

public class NewTranscriptSketch extends JFrame {

	private JPanel contentPane;
	private static JTextField textField;
	private static JTextField textField_1;
	private static JTextField textField_2;
    private JFrame mainFrame;
    private final Action action = new SwingAction();
    JFileChooser fileChooser;
    static String mediaPath;
    static String transcriptPath;
    static Transcriber5 transcriber5;
    static JComboBox comboBox = null;
    static JComboBox comboBox_1 = null;
    static JCheckBox chckbxUseMediaFile ;
    JButton btnBrowse;
    JButton outputBrowseButton;
    static JButton btnNewButton;
    // media title
    
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					NewTranscriptSketch frame = new NewTranscriptSketch(null,0,0);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public NewTranscriptSketch(Transcriber5 transcriber5, double xPos, double yPos) {
		
		this.transcriber5 = transcriber5;
	    this.setVisible(true);
		this.setBounds((int) xPos+80, (int) yPos+80, 350, 400);
	    
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		textField = new JTextField();
		textField.setColumns(10);
		
		fileChooser = new JFileChooser();
		
		btnBrowse = new JButton("Browse");
		//btnBrowse.setAction(action);
		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				fileChooser.setApproveButtonText("Play");
				fileChooser.addChoosableFileFilter(SwingFileFilterFactory
						.newVideoFileFilter());
				fileChooser.addChoosableFileFilter(SwingFileFilterFactory
						.newAudioFileFilter());
				javax.swing.filechooser.FileFilter defaultFilter = SwingFileFilterFactory.newMediaFileFilter();
				fileChooser.addChoosableFileFilter(defaultFilter);
				fileChooser.setFileFilter(defaultFilter);

				
				if (JFileChooser.APPROVE_OPTION == fileChooser
						.showOpenDialog(NewTranscriptSketch.this)) {
					
					mediaPath = fileChooser.getSelectedFile().getAbsolutePath();
					textField.setText(mediaPath);
					// remove file filter
					fileChooser.removeChoosableFileFilter(defaultFilter);
					

				}
				
				
			}
		});
		

		chckbxUseMediaFile = new JCheckBox("Use media file path");
		chckbxUseMediaFile.setAction(action);
		chckbxUseMediaFile.setSelected(true);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setEnabled(false);
		
		
		outputBrowseButton = new JButton("Browse");
		outputBrowseButton.setEnabled(false);
		outputBrowseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// opening a window to let a user choose the file
				JFileChooser c = new JFileChooser();
				// Demonstrate "Save" dialog:
				int rVal = c.showSaveDialog(NewTranscriptSketch.this);
				if (rVal == JFileChooser.APPROVE_OPTION) {
					transcriptPath = c.getSelectedFile().toString();
					textField_1.setText(c.getSelectedFile().toString());
					
					
				}
				if (rVal == JFileChooser.CANCEL_OPTION) {
					System.out.println("You pressed cancel");
				}
			}
		});
		
		JLabel lblSpecifyMainLanguage = new JLabel("Predominant language:");
		
		JLabel lblSelectMediaType = new JLabel("Media type:");
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"<select language>", "Deutsch", "English", "espa\u00F1ol", "\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AC", "fran\u00E7ais", "italiano", "Nederlands", "Norsk", "Pycc\u0138\u0438\u0439", "Sloven\u0161\u010Dina", "Other"}));
		
		comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"<select type>", "--- Video ---", "Movie", "TV Series", "Video Podcast", "Music Video", "--- Audio only ---", "Audiobook", "Song", "Audio Podcast"}));
		
		JLabel lblSelectMediaFile = new JLabel("(1)   Select media file:");
		lblSelectMediaFile.setFont(new Font("Tahoma", Font.BOLD, 11));
		
		JSeparator separator = new JSeparator();
		
		JLabel lblSetProperties = new JLabel("(2)   Set properties:");
		lblSetProperties.setFont(new Font("Tahoma", Font.BOLD, 11));
		
		JSeparator separator_1 = new JSeparator();
		
		JLabel lblSelectOutput = new JLabel("(3) Select output path");
		lblSelectOutput.setFont(new Font("Tahoma", Font.BOLD, 11));

		textField_2 = new JTextField();
		textField_2.setColumns(10);


		// the big button
		JButton btnNewButton = new JButton("Start transcribing!");
		// disabling button if any of the required fields are empty, enable otherwise
		if (this.textField.getText().equals("") |
			this.textField_2.getText().equals("") |
			(this.textField_1.getText().equals("") & !this.chckbxUseMediaFile.isSelected())) {
			//btnNewButton.setEnabled(false);
		} else { btnNewButton.setEnabled(true); }
		// passing results if 'Start transcribing' is clicked
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				NewTranscriptSketch.passResults();
				// pulling the plug
		        WindowEvent wev = new WindowEvent(NewTranscriptSketch.this, WindowEvent.WINDOW_CLOSING);
		        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
			}
		});
		
		JLabel lblMediaTitle = new JLabel("Media title:");
		
		
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(10)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(outputBrowseButton))
								.addComponent(chckbxUseMediaFile)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(10)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblSpecifyMainLanguage)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(comboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblSelectMediaType)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblMediaTitle, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(10)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnBrowse))
						.addComponent(separator, GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
						.addComponent(lblSetProperties)
						.addComponent(lblSelectMediaFile)
						.addComponent(lblSelectOutput)
						.addComponent(separator_1, GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
						.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblSelectMediaFile)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnBrowse))
					.addGap(11)
					.addComponent(separator, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE)
					.addGap(8)
					.addComponent(lblSetProperties)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMediaTitle)
						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSelectMediaType)
						.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSpecifyMainLanguage)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
					.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblSelectOutput)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(chckbxUseMediaFile)
					.addGap(7)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(outputBrowseButton))
					.addGap(18)
					.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		contentPane.setLayout(gl_contentPane);
		
		
		
	} // end of constructor
	
	/**
	 * @wbp.parser.constructor
	 */
	public NewTranscriptSketch(Transcriber5 transcriber5, double xPos, double yPos, Transcript t, String transcriptPath) {
		
		this(transcriber5, xPos,  yPos);
		
		// populate the fields
		// media path
		if ((t.getFilepath()!=null)) {
			this.textField.setText(t.getFilepath());
		}
		// title
		if (t.getTitle()!=null) {
			this.textField_2.setText(t.getTitle());
		}
		// language
		if (t.getLanguage()!=null) {
			this.comboBox.setSelectedItem(t.getLanguage());
		}
		// media type
		if (t.getType()!=null) {
			this.comboBox_1.setSelectedItem(t.getType());
		}
		
		// file path 
		this.textField_1.setText(transcriptPath);
		this.textField_1.setEnabled(true);
		this.outputBrowseButton.setEnabled(true);
		this.chckbxUseMediaFile.setSelected(false);
		
		// disabling button if any of the required fields are empty, enable otherwise
		if (this.textField.getText().equals("") |
			this.textField_2.getText().equals("") |
			(this.textField_1.getText().equals("") & !this.chckbxUseMediaFile.isSelected())) {
			//this.btnNewButton.setEnabled(false);
		} else { this.btnNewButton.setEnabled(true); }

	}
	
	/**
	 * collects the data about a transcript from the fields and
	 * passes it to the transcriber.
	 */
	public static void passResults() {
		String language = (String) comboBox.getSelectedItem();
	    String mediaType = (String) comboBox_1.getSelectedItem();
	    String mediaTitle = (String) textField_2.getText();
	    mediaPath = (String) textField.getText();
	    
	    if (chckbxUseMediaFile.isSelected()) {
	    	// extract the filepath until the file extension
	    	transcriptPath=mediaPath.substring(0, mediaPath.lastIndexOf('.')) +".xml";
	    } else {
	    	transcriptPath=(String) textField_1.getText();
	    }
	    
		transcriber5.setUpTranscript(mediaPath, 
				transcriptPath, 
				language,
				mediaType,mediaTitle);
		
	}
	
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "Use media path");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			if (chckbxUseMediaFile.isSelected()) {
				// disable the text area and the button
				outputBrowseButton.setEnabled(false);
				textField_1.setEnabled(false);
			} else {
				// enable them
				outputBrowseButton.setEnabled(true);
				textField_1.setEnabled(true);
			}
			
			
			
		}
	}
}
